/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2021年10月29日
 */
package com.massyframework.beanskin.customer;

import java.util.Objects;

/**
 * 客户是指使用massyframework平台的组织或者单位，平台会为每个客户分配唯一的编号。
 * <p>
 * 当客户编号为0时，表示未待认证的客户。
 * 在www.massyframework.com实名注册身份后，平台会为客户分配全球唯一的编号，
 * 并对客户信息进行数字签名。
 * @author huangkh
 *
 */
public class Customer{
	
	private final long id;
	private final String name;
	private final String shortName;
	private final String signature;

	/**
	 * 使用{@code builder}创建新实例
	 * @param builder {@link Builder},构建器
	 */
	private Customer(Builder builder) {
		this.id = builder.id == null ? 0 : builder.id.longValue();
		this.name = this.id == 0 ? "待认证客户" : Objects.requireNonNull(builder.name, "\"name\" cannot be null.");
		this.shortName = builder.shortName;
		this.signature = builder.id == 0 ? null : builder.signature;
	}
	
	/**
	 * 编号，具有唯一性.
	 * @return {@link String}
	 */
	public long getId() {
		return this.id;
	}
	
	/**
	 * 全称
	 * @return {@link String}
	 */
	public String getName() {
		return this.name;
	}
	
	/**
	 * 简称
	 * @return {@link String},可能返回null.
	 */
	public String getShortName() {
		return this.shortName;
	}
	
	/**
	 * 签名
	 * @return {@link String}
	 */
	public String getSignature() {
		return this.signature;
	}
	
	

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (id ^ (id >>> 32));
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Customer other = (Customer) obj;
		if (id != other.id)
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Customer [id=" + id + ", name=" + name + "]";
	}
	
	/**
	 * 新客户
	 * @param id {@link Long}，客户编号
	 * @param name {@link String},全称
	 * @param shortName {@link String},简称
	 * @param signature {@link String},签名
	 * @return {@link Customer}
	 */
	public static Customer newCustomer(Long id, String name, String shortName, String signature) {
		return newBuilder()
				.id(id)
				.name(name)
				.shortName(shortName)
				.signature(signature)
				.build();
	}
	
	/**
	 * 创建新的构建器
	 * @return {@link Builder}
	 */
	public static Builder newBuilder() {
		return new Builder();
	}

	/**
	 * 构建器
	 * @author huangkh
	 */
	public static class Builder{
		private Long id;
		private String name;
		private String shortName;
		private String signature;
		
		/**
		 * 设置编号
		 * @param value {@link Long}
		 * @return {@link Builder}
		 */
		public Builder id(Long value) {
			this.id = value;
			return this;
		}
		
		/**
		 * 设置全称
		 * @param value {@link String}
		 * @return {@link Builder}
		 */
		public Builder name(String value) {
			this.name = value;
			return this;
		}
		
		/**
		 * 设置简称
		 * @param value {@link String}
		 * @return {@link Builder}
		 */
		public Builder shortName(String value) {
			this.shortName = value;
			return this;
		}
		
		/**
		 * 设置签名
		 * @param value {@link String}
		 * @return {@link Builder}
		 */
		public Builder signature(String value) {
			this.signature = value;
			return this;
		}
		
		/**
		 * 构建客户实例
		 * @return {@link Customer}
		 */
		public Customer build() {
			return new Customer(this);
		}
	}
}
